package com.supcalls;

import java.util.*;
import java.util.stream.Collectors;

public class StreamMapExample {
    public static void main(String[] args) {
        // 示例 1：List 使用 map() 进行转换
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
        List<String> upperCaseNames = names.stream()
                .map(String::toUpperCase) // 转换为大写
                .collect(Collectors.toList());

        System.out.println("原始 List: " + names);
        System.out.println("转换后的 List: " + upperCaseNames);

        // 示例 2：Map 使用 map() 处理值
        Map<String, Integer> scores = new HashMap<>();
        scores.put("Alice", 85);
        scores.put("Bob", 90);
        scores.put("Charlie", 78);

        Map<String, Integer> updatedScores = scores.entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> entry.getValue() + 10 // 分数 +10
                ));

        System.out.println("原始分数: " + scores);
        System.out.println("加分后的分数: " + updatedScores);

        // 示例 3：筛选 Map，只保留分数 ≥ 80 的学生
        Map<String, Integer> filteredScores = scores.entrySet().stream()
                .filter(entry -> entry.getValue() >= 80)
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

        System.out.println("筛选后的分数: " + filteredScores);

        // 示例 4：提取 Map 的 Key 和 Value
        List<String> studentNames = scores.keySet().stream().collect(Collectors.toList());
        List<Integer> studentScores = scores.values().stream().collect(Collectors.toList());

        System.out.println("所有学生姓名: " + studentNames);
        System.out.println("所有学生分数: " + studentScores);

        // 示例 5：计算 Map 的最高分、最低分和平均分
        Optional<Map.Entry<String, Integer>> maxScore = scores.entrySet().stream()
                .max(Map.Entry.comparingByValue());
        maxScore.ifPresent(entry ->
                System.out.println("最高分: " + entry.getKey() + " - " + entry.getValue()));

        Optional<Map.Entry<String, Integer>> minScore = scores.entrySet().stream()
                .min(Map.Entry.comparingByValue());
        minScore.ifPresent(entry ->
                System.out.println("最低分: " + entry.getKey() + " - " + entry.getValue()));

        OptionalDouble averageScore = scores.values().stream()
                .mapToInt(Integer::intValue)
                .average();
        System.out.println("平均分: " + (averageScore.isPresent() ? averageScore.getAsDouble() : "无数据"));
    }
}
